﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Sale
{
    public partial class YuanLiao_DaoHuo_Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            String sql = @"Select distinct [管材到货登记].[ID],[单据号],[管材到货登记].[车号],原料供货单位.名称,sum([管材到货明细].重量) as 名义重量合计,sum([管材到货明细].磅重) as 实际重量合计,sum([管材到货明细].支数) as 实际支数合计
                            ,[管材到货登记].[到货日期] from [管材到货登记] inner join [管材到货明细] on 
                            [管材到货登记].ID=[管材到货明细].管材到货登记ID inner join 原料供货单位 on 
                            原料供货单位.ID=[管材到货登记].原料供货单位ID  inner join 原料 on 原料.ID=[管材到货明细].[管材ID]
                            where     原料.ID='" + Request.QueryString["GuanCaiID"] + "'  group by [管材到货登记].[ID],[单据号],[管材到货登记].[车号],原料供货单位.名称,[管材到货登记].[到货日期] order by [管材到货登记].[到货日期] desc";           
            
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
           // string temp = this.DanHao.Text.Trim().Replace("'", "");
            string Sql = "";
            Sql = "[单据号] like '%%'";

            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and [管材到货登记].[到货日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and [管材到货登记].[到货日期]<='" + Dt.ToShortDateString() + "'";
            }
            if (this.GongHuoDanWei.Text != "0")
            {
                Sql += " and [管材到货登记].[原料供货单位ID]=" + this.GongHuoDanWei.Text;
            }
            if (this.YuanCaiLiao.Text != "0")
            {
                Sql += " and [管材到货明细].[管材ID]=" + this.YuanCaiLiao.Text;
            }
            this.SqlDataSource1.SelectCommand = @"Select distinct [管材到货登记].[ID],[单据号],[管材到货登记].[车号],原料供货单位.名称,sum([管材到货明细].重量) as 名义重量合计,sum([管材到货明细].磅重) as 实际重量合计,sum([管材到货明细].支数) as 实际支数合计
                            ,[管材到货登记].[到货日期] from [管材到货登记] inner join [管材到货明细] on 
                            [管材到货登记].ID=[管材到货明细].管材到货登记ID inner join 原料供货单位 on 
                            原料供货单位.ID=[管材到货登记].原料供货单位ID where " + Sql + " group by [管材到货登记].[ID],[单据号],[管材到货登记].[车号],原料供货单位.名称,[管材到货登记].[到货日期] order by [管材到货登记].[到货日期] desc";

            this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;
        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            string GuanCaiID = Request.QueryString["GuanCaiID"];
            if (GuanCaiID == "1")
            {
                Response.Redirect("油管管材到货登记.aspx?GuanCaiID=1");
            }
            if (GuanCaiID == "2")
            {
                Response.Redirect("套管管材到货登记.aspx?GuanCaiID=2");
            }
            if (GuanCaiID == "3")
            {
                Response.Redirect("油管接箍到货登记.aspx?GuanCaiID=3");
            }
            if (GuanCaiID == "4")
            {
                Response.Redirect("套管接箍到货登记.aspx?GuanCaiID=4");
            }
            if (GuanCaiID == "6")
            {
                Response.Redirect("套管成品接箍登记.aspx?GuanCaiID=6");
            }
           // Response.Redirect("YuanLiao_DaoHuo_Register.aspx");
        }

        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }
        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("YuanLiao_DaoHuo_Register.aspx?ID=" + ((Button)sender).CommandName);
        //}
        //protected void Button2_Click(object sender, EventArgs e)
        //{
        //    this.SqlDataSource1.DeleteCommand = "Exec GuanCai_DaoHuo_Delete " + ((Button)sender).CommandName;
        //    this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
        //    this.SqlDataSource1.Delete();
        //    this.GridView1.DataBind();
        //}

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                string GuanCaiID = Request.QueryString["GuanCaiID"];
                if (GuanCaiID == "1")
                {
                    Response.Redirect("油管管材到货登记.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString() + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);
                }
                if (GuanCaiID == "2")
                {
                    Response.Redirect("套管管材到货登记.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString() + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);
                }
                if (GuanCaiID == "3")
                {
                    Response.Redirect("油管接箍到货登记.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString() + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);               
                }
                if (GuanCaiID == "4")    
                {
                    Response.Redirect("套管接箍到货登记.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString() + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);               
                }
                if (GuanCaiID == "6")
                {
                    Response.Redirect("套管成品接箍登记.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString() + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);              
                }
               
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                this.SqlDataSource1.DeleteCommand = "Exec GuanCai_DaoHuo_Delete  " + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
                this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource1.Delete();
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }
        ////明细
        protected void Button_MX_Click(object sender, EventArgs e)
        {
            string id = ((Button)sender).CommandName;
           // Response.Redirect("/Sale/YuanLiaoDaoHuo_MingXi.aspx?id=" + id + "&GuanCai=" + Request.QueryString["GuanCai"]);
            string GuanCaiID = Request.QueryString["GuanCaiID"];
            if (GuanCaiID == "1")
            {
                Response.Redirect("/Sale/油管管材到货登记_read.aspx?id=" + id + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);
            }
            if (GuanCaiID == "2")
            {
                Response.Redirect("/Sale/套管管材到货登记_read.aspx?id=" + id + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);
            }
            if (GuanCaiID == "3")
            {
                Response.Redirect("/Sale/油管接箍到货登记_read.aspx?id=" + id + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);
            }
            if (GuanCaiID == "4")
            {
                Response.Redirect("/Sale/套管接箍到货登记_read.aspx?id=" + id + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);
            }
            if (GuanCaiID == "6")
            {
                Response.Redirect("/Sale/套管成品接箍登记_read.aspx?id=" + id + "&GuanCaiID=" + Request.QueryString["GuanCaiID"]);
            }
           
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.GongHuoDanWei.SelectedIndex = 0;
            this.YuanCaiLiao.SelectedIndex = 0;
            this.Date1.Text = "";
            this.Date2.Text = "";
        }
    }
}
